package com.youlin;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.quartz.CronScheduleBuilder.cronSchedule;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BootQuart {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Test
    public void test11 () throws Exception {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        //创建一个Trigger
        TriggerKey triggerKey1 = new TriggerKey("helloQuartzTrigger", "helloTriggerGroup");
        Trigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey1)
                .withSchedule(cronSchedule("0/3 26 * * * ?"))
                .build();

        //创建一个job
        JobKey jobKey1 = new JobKey("helloQuartzJob-1", "helloJobGroup-1");
        JobDetail jobDetail1 = JobBuilder
                .newJob(HelloQuartzJob.class)
                .withIdentity(jobKey1)
                .usingJobData("jk0", 2)
                .build();

        TriggerKey triggerKey2 = new TriggerKey("helloQuartzTrigger2", "helloTriggerGroup");
        Trigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity(triggerKey2)
                .withSchedule(cronSchedule("0/3 26 * * * ?"))
                .build();

        JobDetail jobDetail2 = JobBuilder
                .newJob(HelloQuartzJob.class)
                .withIdentity("helloQuartzJob-2", "helloJobGroup-2")
                .usingJobData("jk0", 2)
                .build();

        Map<JobDetail, Set<? extends Trigger>> triggersAndJobs = new HashMap(){{
            put(jobDetail1, new HashSet<Trigger>(){{add(trigger1);}});
            put(jobDetail2, new HashSet<Trigger>(){{add(trigger2);}});
        }};
        scheduler.scheduleJobs(triggersAndJobs, false);
        scheduler.start();

        TimeUnit.SECONDS.sleep(1234);
    }


}
